package com.design.BuilderMode.demo;


import lombok.Data;
import lombok.ToString;
import org.springframework.util.StringUtils;

@Data
@ToString
public class ThreadConfig {

    /**
     * 核心线程默认值
     */
    private static final Integer CORE_POOL_SIZE = 4;

    /**
     * 线程名称
     */
    private String threadName;

    /**
     * 核心线程数
     */
    private Integer corePoolSize = CORE_POOL_SIZE;
    /**
     * 最大线程数
     */
    private Integer maxPoolSize;
    /**
     * 队列数
     */
    private Integer queueCapacity;

    /**
     * 当线程空闲时间达到keepAliveTime，该线程会退出
     */
    private Integer keepAliveTime;

    /**
     * 是否允许核心线程数空闲时退出
     */
    private boolean allowCoreThreadTimeout;

    public ThreadConfig(String threadName,Integer corePoolSize,Integer maxPoolSize,Integer queueCapacity,Integer keepAliveTime) throws IllegalAccessException {
        if(StringUtils.isEmpty(threadName)){
            throw  new IllegalAccessException("线程名不能为空！");
        }
        this.threadName = threadName;
        if(null != corePoolSize ){
            if( corePoolSize <= 0){
                throw  new IllegalAccessException("核心线程数不能小于等于0！");
            }
            this.corePoolSize = corePoolSize;
        }
        if(null != maxPoolSize  ){
            if(maxPoolSize < this.corePoolSize){
                throw  new IllegalAccessException("最大线程数不能小于核心线程数！");
            }
            this.maxPoolSize = corePoolSize;
        }
        if(null != queueCapacity ){
            if( queueCapacity <= 0 ){
                throw  new IllegalAccessException("队列书不能小于等于0！");
            }
            this.queueCapacity = queueCapacity;
        }
        if(null != keepAliveTime  ){
            if( keepAliveTime <= 0 ){
                throw  new IllegalAccessException("空闲时间不能小于等于0！");
            }
            this.keepAliveTime = keepAliveTime;
        }
    }
}
